﻿using LaserTag.Infrastructure;
using LaserTag.Infrastructure.Events;
using LaserTag.Module.AForgeModule.ViewModels;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;

namespace LaserTag.Module.AForgeModule.Controllers
{
    /// <summary>
    /// The AForgeVideoProcessorController controls initialization of all video processing
    /// services of this module
    /// </summary>
    public class AForgeVideoProcessorController
    {
        private readonly IRegionManager m_regionManager;
        private readonly IAForgeVideoProcessorSetupViewModel m_viewModel;
        private IEventAggregator m_eventAggregator;

        /// <summary>
        /// Creates a new instance of the AForgeVideoProcessorController.  This is instantiated
        /// via dependency injection.  All constructor arguments are resolved by the dependency
        /// injection container.  This class should not be directly instantiated.
        /// </summary>
        public AForgeVideoProcessorController(IUnityContainer container, 
                                              IRegionManager regionManager, 
                                              IAForgeVideoProcessorSetupViewModel viewModel,
                                              IEventAggregator eventAggregator)
        {
            m_eventAggregator = eventAggregator;
            m_viewModel = viewModel;
            m_regionManager = regionManager;

            eventAggregator.GetEvent<SetupCategorySelected>().Subscribe(OnSetupCategorySelected, true);
        }

        private void OnSetupCategorySelected(SetupCategory category)
        {
            if (category != SetupCategory.VisionSetup)
                return;

            m_regionManager.Regions[RegionNames.SetupRegion].Activate(m_viewModel.View);
        }

        /// <summary>
        /// Callback method for the region manager to get the video processor view
        /// </summary>
        /// <returns></returns>
        private object GetVideoProcessorSetupView()
        {
            /* We want to return the view associated to our view model */
            return m_viewModel.View;
        }

        public void Run()
        {
            /* Wee register a specific region name with our callback.  So when Prism finds Region.RegionName="xyz"
             * it will automatically add our view to that specified area */
            m_regionManager.RegisterViewWithRegion(RegionNames.SetupRegion, GetVideoProcessorSetupView);
        }
    }
}
